# -*- coding: utf-8 -*-
from scrapy import Spider, Request
import json
from Mweibo.items import *

class MweibocnSpider(Spider):
    name = 'mweibocn'
    allowed_domains = ['m.weibo.cn']

    # 用户详情API（点击用户头像或者昵称进入的页面，其最上方西安市的信息）
    user_url = 'https://m.weibo.cn/api/container/getIndex?uid={uid}&type=uid&value={uid}&containerid=100505{uid}'
    # 关注列表API
    follow_url = 'https://m.weibo.cn/api/container/getIndex?containerid=231051_-_followers_-_{uid}&page={page}'
    # 粉丝列表API（已改变，原来的也能用？）
    # 'https://m.weibo.cn/api/container/getIndex?containerid=231051_-_fans_-_2559339793&since_id=2'
    fan_url = 'https://m.weibo.cn/api/container/getIndex?containerid=231051_-_fans_-_{uid}&since_id={page}'
    # 微博列表API(原接口能用,不明出处)
    # 接口2：'https://m.weibo.cn/api/container/getIndex?uid=1650111241&t=0&luicode=10000011&lfid=100103type%3D1%26q%3D%E4%B8%AD%E5%9B%BD%E7%BB%8F%E8%90%A5%E6%8A%A5&type=uid&value=1650111241&containerid=1076031650111241&since_id=4467969596296046'
    weibo_url = 'https://m.weibo.cn/api/container/getIndex?uid={uid}&type=uid&page={page}&containerid=107603{uid}'

    # 金猪下崽M,
    start_users = ['5586408376', ]

    def start_requests(self):
        for uid in self.start_users:
            yield Request(self.user_url.format(uid=uid), callback=self.parse_user)

    def parse_user(self, response):
        """
        解析用户信息
        :param response:Response对象
        :yield:
        """
        # self.logger.debug(response)
        result = json.loads(response.text)
        if result.get('data').get('userInfo'):
            user_info = result.get('data').get('userInfo')
            user_item = UserItem()
            # 定义字段映射关系，处理自定义字段名和JSON中用户的字段名不同的情况，然后遍历字典实现赋值
            field_map = {
                'id': 'id',
                'name': 'screen_name',
                'avatar': 'profile_image_url',
                'cover': 'cover_image_phone',
                'gender': 'gender',
                'description': 'description',
                'fans_count': 'followers_count',
                'follows_count': 'follow_count',
                'weibos_count': 'statuses_count',
                'verified': 'verified',
                'verified_reason': 'verified_reason',
                'verified_type': 'verified_type'
            }
            for field, attr in field_map.items():
                user_item[field] = user_info.get(attr)
            yield user_item
            # 关注
            uid = user_info.get('id')
            yield Request(
                self.follow_url.format(uid=uid, page=1),
                callback=self.parse_follows,
                meta={'page': 1, 'uid': uid}
            )
            # 粉丝
            yield Request(
                self.fan_url.format(uid=uid, page=1),
                callback=self.parse_fans,
                meta={'page':1, 'uid':uid}
            )
            # 微博
            yield Request(
                self.weibo_url.format(uid=uid, page=1),
                callback=self.parse_weibos,
                meta={'page': 1, 'uid': uid}
            )

    def parse_follows(self, response):
        """
        解析用户关注
        :param response: Response对象
        """
        result = json.loads(response.text)
        # 这里就在判断有无下一页。有result.get('ok')返回的就是1；没有就是0
        if result.get('ok') \
                and result.get('data').get('cards') \
                and len(result.get('data').get('cards')) \
                and result.get('data').get('cards')[-1].get('card_group'):
            # 解析用户
            follows = result.get('data').get('cards')[-1].get('card_group')
            for follow in follows:
                if follow.get('user'):
                    uid = follow.get('user').get('id')
                    yield Request(self.user_url.format(uid=uid), callback=self.parse_user)
            # 关注列表
            uid = response.meta.get('uid')
            user_relation_item = UserRelationItem()
            follows = [{'id': follow.get('user').get('id'), 'name': follow.get('user').get('screen_name')} for follow in follows]
            user_relation_item['id'] = uid
            user_relation_item['follows'] = follows
            user_relation_item['fans'] = []
            yield user_relation_item
            # 下一页关注
            page = response.meta.get('page') + 1
            yield Request(
                self.follow_url.format(uid=uid, page=page),
                callback=self.parse_follows,
                meta={'page': page, 'uid': uid}
            )

    def parse_fans(self, response):
        """
        解析用户粉丝
        :param response: Response对象
        """
        result = json.loads(response.text)
        if result.get('ok') \
                and result.get('data').get('cards') \
                and len(result.get('data').get('cards')) \
                and result.get('data').get('cards')[-1].get('card_group'):
            # 解析用户
            fans = result.get('data').get('cards')[-1].get('card_group')
            for fan in fans:
                if fan.get('user'):
                    uid = fan.get('user').get('id')
                    yield Request(self.user_url.format(uid=uid), callback=self.parse_user)

            uid = response.meta.get('uid')
            # 粉丝列表
            user_relation_item = UserRelationItem()
            fans = [{'id': fan.get('user').get('id'), 'name': fan.get('user').get('screen_name')} for fan in
                    fans]
            user_relation_item['id'] = uid
            user_relation_item['fans'] = fans
            user_relation_item['follows'] = []
            yield user_relation_item
            # 下一页粉丝
            page = response.meta.get('page') + 1
            yield Request(
                self.fan_url.format(uid=uid, page=page),
                callback=self.parse_fans,
                meta={'page': page, 'uid': uid}
            )

    def parse_weibos(self, response):
        """
        解析微博列表
        :param response: Response对象
        """
        result = json.loads(response.text)
        if result.get('ok') and result.get('data').get('cards'):
            weibos = result.get('data').get('cards')
            for weibo in weibos:
                mblog = weibo.get('mblog')
                if mblog:
                    weibo_item = WeiboItem()
                    field_map = {
                        'id': 'id',
                        'attitudes_count': 'attitudes_count',
                        'comments_count': 'comments_count',
                        'reposts_count': 'reposts_count',
                        'picture': 'original_pic', 'pictures': 'pics',
                        'created_at': 'created_at',
                        'source': 'source',
                        'text': 'text',
                        # 'raw_text': 'raw_text',
                        'thumbnail': 'thumbnail_pic',
                    }
                    for field, attr in field_map.items():
                        weibo_item[field] = mblog.get(attr)
                    weibo_item['user'] = response.meta.get('uid')
                    yield weibo_item
            # 下一页微博
            uid = response.meta.get('uid')
            # uid = '2106083604'
            page = response.meta.get('page') + 1
            # page = 2
            yield Request(self.weibo_url.format(uid=uid, page=page), callback=self.parse_weibos,
                          meta={'uid': uid, 'page': page})

'''
注意1：weibo_url如果使用接口2
    def parse_weibos(self, response):
        ...
        if result.get('ok') \
                    and result.get('data').get('cards') \
                    and len(result.get('data').get('cards')):
                
    要加长度判断
'''